<?php
if (!defined('ZZQSS')){
	die("Access Denied");
}
/**
 * GroupBondClass類
 * add()
 * adds()
 * edit()
 * del()
 * getdata()
 * 
 *
 */
class GroupBondClass {
	var $db;
	var $table;
	var $tablepre;
	function __construct(){
		//設置全局變量
		global $db,$tablepre;
		$this->db=$db;
		$this->tablepre=$tablepre;
		$this->table=$tablepre."groupbond";
	}
	function GroupBondClass() {
		$this->__construct();
	}
	/**
	 * 添加網站消費卷
	 *
	 * @param unknown_type $gid
	 * @param unknown_type $sn
	 * @param unknown_type $password
	 * @param unknown_type $endtime
	 * @param unknown_type $state
	 * @return unknown
	 */
	function add($gid,$sn="",$password="",$endtime=0,$state=0){
		if(is_numeric($gid) && !empty($sn)){
			$row = $this->db->fetch_first("Select sid,title From {$this->tablepre}groups where gid = {$gid}");
			if(is_array($row)){
				$this->db->query("insert into {$this->table}(gid,sid,gtitle,sn,password,endtime,state) VALUES({$gid},'{$row["sid"]}','{$row[title]}','{$sn}','{$password}','{$endtime}',$state)");												
				return "OK";
			}else {
				return "團購ID不存在";
			}
		}else {
			return "參數錯誤";	
		}

	}
	/**
	 * 批量添加消費卷
	 *
	 * @param unknown_type $gid
	 * @param unknown_type $num
	 * @param unknown_type $pre
	 * @param unknown_type $len
	 * @param unknown_type $endtime
	 * @param unknown_type $autopwd
	 * @param unknown_type $type
	 * @return unknown
	 */
	function adds($gid,$num,$pre="",$len=10,$endtime=0,$autopwd=1,$type="ALL"){
		if(is_numeric($gid) && $num>0){
			for ($i=0;$i<$num;$i++){
				$autokeys=strtoupper(randomkeys($len-strlen($pre)));//隨機生成轉成大寫
				$sn=$pre.$autokeys;
				$password=randomkeys(4);//隨機產生4位密碼
				$this->add($gid,$sn,$password,$endtime,0);//調用單個添加完成批量加入
				
			}
			return "OK";
		}else return "缺少參數";
	}
	/**
	 * 修改購物卷信息
	 *
	 * @param int $bid
	 * @param array $barray
	 * @return OK /提示
	 */
	function edit($bid,$barray){
		if(is_array($barray) && is_numeric($bid)){
			$row = $this->db->fetch_first("Select sid,title From {$this->tablepre}groups where gid = {$barray[gid]}");
			if(is_array($row)){
				//更新數據庫操作	
				
				$barray['sid']=$row['sid'];
				$barray['gtitle']=$row['title'];
				
				$wheresqlarr=" bid='{$bid}'";
				updatetable($this->table,$barray, $wheresqlarr);
				return "OK";
			}else return "團購ID不存在";
		}else return "參數錯誤";
	}
	
	function binduser($uid,$uname,$order_sn,$gid,$buynum=1){
		if (!empty($uid) && !empty($uname) && !empty($order_sn) && !empty($gid)) {
			$bids=array();
			if ($buynum==1) {
				$gpdata=$this->getdata(1,"state=0 and uid=0 and gid={$gid}");
				$bid=$gpdata[0]['bid'];
			}elseif ($buynum>1){
				$gpdata=$this->getdata($buynum,"state=0 and uid=0 and gid={$gid}");
				foreach ($gpdata as $val){
					$bids[]=$val['bid'];
				}
				$bid=getdotstring($bids,'int');
			}
			if (GetNum($bid)) {
				$sql="update {$this->table} set uid='{$uid}',uname='{$uname}',order_sn='{$order_sn}',state=1 where bid in({$bid})";
				$this->db->query($sql);
				return "OK";
			}else return "暫時沒有符合條件的團購卷分配,請等待管理員分配";
			
		}else return "缺少參數";
	}
	
	/**
	 * 刪除指定ID消費卷
	 *
	 * @param int $bid
	 * @return true/false
	 */
	function del($bid){
		if(is_numeric($bid)){
			$this->db->query("delete from {$this->table} where bid='{$bid}'");
			return true;			
		}return false;
	}
	
	/**
	 *  更改團購卷的狀態
	 *
	 *
	 *
	**/
	function updateState($bid,$state){
		if(is_numeric($bid) && is_numeric($state)){
			$this->db->query("UPDATE {$this->table} SET `state` = '{$state}' WHERE `bid` ='{$bid}'");
			return true;			
		}return false;
	}
	/**
	 *  更改團購卷的使用者狀態
	 *   user_state
	 *
	 *
	**/
	function updateUserState($bid,$state){
		if(is_numeric($bid) && is_numeric($state)){
			$this->db->query("UPDATE {$this->table} SET `user_state` = '{$state}' WHERE `bid` ='{$bid}'");
			return true;			
		}return false;
	}
	
	/**
	 * 獲取一條信息
	 *
	 * @param unknown_type $oid
	 * @return unknown
	 */
	function getone($bid,$type="ALL"){
		if (GetNum($bid)) {
			$gpdata=$this->getdata(1,"bid='{$bid}'","",$type);
			$value=$gpdata[0];
		}
		return $value;
	}
	/**
	 * byordersn獲取一條信息
	 *
	 * @param unknown_type $oid
	 * @return unknown
	 */
	function getbyordersn($order_sn,$type="ALL"){
		if (GetNum($order_sn)) {
			$value=$this->getdata($limit="","order_sn='{$order_sn}'","",$type);
		}
		return $value;
	}
	
	/**
	 * 獲取數據數組
	 *
	 * @param string $limit
	 * @param string $where
	 * @param string $orderby
	 * @return array
	 */
	function getdata($limit="",$where="",$orderby="",$type="ALL"){
		$tempdata=array();
		if(!empty($limit))$limit=" limit $limit ";
		if(!empty($where))$where=" where $where ";
		if(!empty($orderby))$orderby=" order by $orderby ";else $orderby=" order by bid desc";
		if ($type=="ALL")$addsql=" AS B left join {$this->tablepre}suppliers AS S ON B.sid=S.sid ";
		$sql="select * from {$this->table}{$addsql}{$where}{$orderby}{$limit}";
		$query =$this->db->query($sql);		
		while($value = $this->db->fetch_array($query)) {
			$tempdata[]=$value;
		}
		return $tempdata;
	
	}
	function getcount($where=""){
	if(!empty($where))$where=" where $where";
	$count= $this->db->result_first("Select count(*) From `$this->table` $where");
	return $count;
	
	}
	
}
?>